<template>
  <div class="login-container">
    医院设置添加<hr>
    <el-form label-width="120px">
      <el-form-item label="医院名称">
        <el-input v-model="hospitalSet.hosname"></el-input>
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="hospitalSet.hoscode"></el-input>
      </el-form-item>
      <el-form-item label="api基础路径">
        <el-input v-model="hospitalSet.apiUrl"></el-input>
      </el-form-item>
      <el-form-item label="联系人">
        <el-input v-model="hospitalSet.contactsName"></el-input>
      </el-form-item>
      <el-form-item label="联系人手机">
        <el-input v-model="hospitalSet.contactsPhone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="saveOrupdate()">保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import hospset from '@/api/hospset'
export default ({
  //任何vue页面都有data()和created()
  data(){
    return{
      hospitalSet:{}
    }
  },
  //created是一个页面渲染
  created() {
    //页面渲染前执行
    //获取路由id 调用接口得到信息
    if(this.$route.params && this.$route.params.id){
      const id=this.$route.params.id
      this.getHospSet(id)
    }else {
      /**
       *清空表单
       * 对于页面重用，需要去 src/views/layout/components/AppMain.vue把
       <router-view :key="key"></router-view>
       与
       key() {
        return this.$route.name !== undefined ? this.$route.name + +new Date() : this.$route + +new Date()
       } 的注释解开
       */
      this.hospitalSet={}
    }
  },
  methods:{
    //根据id查询
    getHospSet(id){
      hospset.getHospSet(id)
      .then(response=>{
        this.hospitalSet=response.data
      })
    },
    //添加
    save(){
      hospset.saveHospitalSet(this.hospitalSet)
      .then(response=>{
        this.$message({
          type: 'success',
          message: '添加成功!'
        });
        //添加成功跳转到列表页面
        this.$router.push({path: '/hospset/list'})
      })
    },
    //修改
    update(){
      hospset.updateHospSet(this.hospitalSet)
        .then(response=>{
          this.$message({
            type: 'success',
            message: '修改成功'
          });
          //修改成功跳转到列表页面
          this.$router.push({path: '/hospset/list'})
        })
    },
    saveOrupdate(){
      //判断是添加还是修改
      if(!this.hospitalSet.id){
        //没有id 添加
        this.save()
      }else {
        this.update()
      }
    }
  }
})
</script>
